package org.suyafei.listener;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.suyafei.enums.BizCodeEnum;
import org.suyafei.enums.EventMessageTypeEnum;
import org.suyafei.exception.BizException;
import org.suyafei.model.EventMessage;
import org.suyafei.service.ShortLinkService;

import java.io.IOException;


/**
 * @author 宿某人
 * @Date: 2022/01/24/3:07 下午
 * @Description:
 */
@Component
@Slf4j
//@RabbitListener(queues = "short_link.add.link.queue")
@RabbitListener(queuesToDeclare = {@Queue("short_link.add.link.queue")})
public class ShortLinkAddLinkMQListener {

    @Autowired
    private ShortLinkService shortLinkService;

    @RabbitHandler
    public void shortLinkHandler(EventMessage eventMessage, Message message, Channel channel) throws IOException {
        log.info("监听到ShortLinkAddLinkMQListener message消息内容:{}", message);
        try {
            eventMessage.setEventMessageType(EventMessageTypeEnum.SHORT_LINK_ADD_LINK.name());
            shortLinkService.handleAddShortLink(eventMessage);
        } catch (Exception e) {
            log.error("消费失败{}", eventMessage);
            throw new BizException(BizCodeEnum.MQ_CONSUME_EXCEPTION);
        }
        log.info("消费成功:{}", eventMessage);
    }
}
